package org.pas.fb2.tag;

import java.io.PrintStream;

public class RootTag extends Tag {

    private final PrintStream out;
    private boolean wasEmpty = true;

    public RootTag() {
        super(null);
        out = System.out;
    }

    public RootTag(PrintStream out) {
        super(null);
        this.out = out;
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public Tag onChild(String name) {
        if (FictionBookTag.NAME.equals(name)) {
            return new FictionBookTag(this);
        }
        throw new IllegalArgumentException("unknown tag " + name);
    }

    @Override
    protected void out(CharSequence sequence) {
        out.println(sequence);
        wasEmpty = sequence.length() == 0;
    }

    @Override
    protected void emptyLine(boolean forced) {
        if (!wasEmpty || forced) {
            out.println();
        }
        wasEmpty = true;
    }
}
